//
//  NSArrayExtensions.m
//  MantuPlay
//
//  Created by Markus Sintonen on 6.6.2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "NSArrayExtensions.h"


@implementation NSArray (NSArrayExtensions)

- (id)objectAtIndexNum:(DBIndex*)i {
	return [self objectAtIndex:[i index]];
}

- (NSArray *)objectsAtIndexesInArray:(NSArray*)indexes {
	NSMutableArray *arr = [NSMutableArray arrayWithCapacity:[indexes count]];
	for(DBIndex *index in indexes) {
		[arr addObject:[self objectAtIndexNum:index]];
	}
	return arr;
}

-(NSArray*)intersectingElementsWithArray:(NSArray*)array {
	unsigned int firstLength = [self count];
	unsigned int secondLength = [array count];
	
	if(firstLength == 0 && secondLength == 0) return [NSArray array];
	
	NSArray *bigger = firstLength > secondLength ? self : array;
    NSArray *smaller = firstLength > secondLength ? array : self;
	NSMutableArray *intersectingElements = [[NSMutableArray alloc] initWithCapacity:[smaller count]];
	
	for(id element in smaller) {
		if([bigger containsObject:element] && ![intersectingElements containsObject:element])
			[intersectingElements addObject:element];
	}
	return [intersectingElements autorelease];
}
@end
